package com.yrihr.wisdom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yrihr.wisdom.dto.NewsDto;
import com.yrihr.wisdom.entity.News;
import com.yrihr.wisdom.mapper.NewsMapper;
import com.yrihr.wisdom.service.NewsSercice;
import com.yrihr.wisdom.vo.NewsTypeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author by Hasee
 * @Classname NewsSerciceImpl
 * @Description
 * @Date 2020/11/9 17:22
 */
@Service
@RequiredArgsConstructor
public class NewsSerciceImpl extends ServiceImpl<NewsMapper, News> implements NewsSercice {
    private final static String TABLE_ALIAS_PREFIX = "news.";


    /**
     * 根据新闻id 进行分类分页
     *
     * @param page
     * @param newsDto
     * @return IPage<News>
     */
    @Override
    public IPage<News> selectPageNews(IPage<?> page,  NewsDto newsDto) {
        QueryWrapper<News> wrapper = Wrappers.<News>query();
        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.between(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime(), newsDto.getEndTime());
        }

        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isBlank(newsDto.getEndTime())) {
            wrapper.ge(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime());
        }
        if (StringUtils.isBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  newsDto.getEndTime());
        }
        if (StringUtils.isNotBlank(newsDto.getNewsTitle())) {
            wrapper.like(TABLE_ALIAS_PREFIX + "newsNm",newsDto.getNewsTitle());
        }
        // 查询更新时间小于当前时间：万一爬取有问题，不至于尴尬
        wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  new Date());
        page.setSize(10);
        if (newsDto.getType()!=null) {
            // 处理全部
            Integer all = 999;
            if (newsDto.getType().equals(all)) {
                wrapper.notIn("proCat", newsDto.getType());
            } else {
            wrapper.in("proCat", newsDto.getType());
            }
        }
        return baseMapper.selectPageVo(page,wrapper);
    }

    /**
     * 首页分页
     *
     * @param page
     * @return
     */
    @Override
    public IPage<News> getNewsPageList(IPage<?> page) {
        page.setSize(10);
        QueryWrapper<News> wrapper = Wrappers.<News>query();
        // 查询更新时间小于当前时间：万一爬取有问题，不至于尴尬
        wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  new Date());
        return baseMapper.getNewsPageList(page,wrapper);
    }

    /**
     * 查询所有新闻类型
     *
     * @return
     */
    @Override
    public List<NewsTypeVo> getNewsAllType(NewsDto newsDto) {
        QueryWrapper<News> wrapper = Wrappers.<News>query();
        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.between(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime(), newsDto.getEndTime());
        }

        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isBlank(newsDto.getEndTime())) {
            wrapper.ge(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime());
        }
        if (StringUtils.isBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  newsDto.getEndTime());
        }
        if (StringUtils.isNotBlank(newsDto.getNewsTitle())) {
            wrapper.like(TABLE_ALIAS_PREFIX + "newsNm",newsDto.getNewsTitle());
        }
        // 查询更新时间小于当前时间：万一爬取有问题，不至于尴尬
        wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  new Date());
        // 处理全部
        Integer all = 999;
        wrapper.notIn("procat", all);
        return baseMapper.getNewsAllType(wrapper);
    }

    /**
     * 查询新闻总条数
     */
    @Override
    public Integer getAllNewsCount(NewsDto newsDto) {
        QueryWrapper<News> wrapper = Wrappers.<News>query();
        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.between(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime(), newsDto.getEndTime());
        }

        if (StringUtils.isNotBlank(newsDto.getStartTime()) && StringUtils.isBlank(newsDto.getEndTime())) {
            wrapper.ge(TABLE_ALIAS_PREFIX + "pubDate", newsDto.getStartTime());
        }
        if (StringUtils.isBlank(newsDto.getStartTime()) && StringUtils.isNotBlank(newsDto.getEndTime())) {
            wrapper.le(TABLE_ALIAS_PREFIX + "pubDate",  newsDto.getEndTime());
        }
        if (StringUtils.isNotBlank(newsDto.getNewsTitle())) {
            wrapper.like(TABLE_ALIAS_PREFIX + "newsNm",newsDto.getNewsTitle());
        }
        return baseMapper.getAllNewsCount(wrapper);
    }

    /**
     * 查询今天的新闻，首页轮播使用
     *
     * @return
     */
    @Override
    public List<News> selectTodayNews() {
        QueryWrapper<News> wrapper = Wrappers.<News>query();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(date);
        // 查询更新时间小于当前时间：万一爬取有问题，不至于尴尬
        wrapper.between(TABLE_ALIAS_PREFIX + "pubDate",today+" 00:00:00",date);
        return baseMapper.selectTodayNews(wrapper);
    }
}
